// pages/goodsDetail/goodsDetail.js
import { request } from "../../utils/index.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: getApp().globalData.baseUrl + "/uploads/",
    imgUrl2: getApp().globalData.baseUrl ,
    info:{}, // 商品详情
    addressList:[], // 地址列表
    address:{}, // 选择的地址信息
    page:1
  },

  // 查询商品详情
  getGoodsDetail (id) {
    var that = this;
    request({ url: "/api/goods/show",data:{id:id} })
    .then(result => {
      console.log(result)
      that.setData({
        info:{
          id:result.data.id,
          type:result.data.type,
          name:result.data.name,
          price:result.data.price,
          desc:result.data.desc,
          image:result.data.image,
          status:result.data.status,
          created_at:result.data.created_at,
          content:result.data.content.replace(/\<img src="/gi,'<img src="'+that.data.imgUrl2)
        }
      })
    })
  },

  // 查询地址列表
  getAddressList () {
    var that = this;
    request({ url: "/api/address/index",data:{page:that.data.page} })
    .then(result => {
      console.log(result)
      var addressList = new Array();
      addressList = result.data.list
      that.setData({addressList:result.data.list})
      if (addressList.length > 0) {
        for (var index in addressList){
          if(addressList[index].is_default == 1) {
              that.setData({
                address:addressList[index]
             })
          }
        }
      } else {
        console.log("未查询到地址信息")
      }
    })
  },
  
  // 选择地址
  onAddress(){
    let str=JSON.stringify(this.data.address);
    if (this.data.addressList.length == 0){
      wx.navigateTo({
        url: '/pages/address/address?type=0',
      })
    }else {
      wx.navigateTo({
        url: '/pages/address/address?type=1&jsonStr='+str,
      })
    }

  //   request({ url: "/api/address/del",data:
  //   {
  //     id:"9"
  //   }
  //  })
  // .then(result => {
  //   wx.showToast({
  //     title: result.msg,
  //     icon: 'none',
  //     duration:2000
  //   });
  // })

  },

  // 点击兑换
  onSubmit(){
    var that = this;
    request({ url: "/api/goods/exchange",data:
      {
        goods_id:that.data.info.id,
        address_id:that.data.address.id
      }
     })
    .then(result => {
      wx.showToast({
        title: result.msg,
        icon: 'none',
        duration:2000
      });
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    this.getGoodsDetail(id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 查询地址列表
    this.getAddressList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})